package com.cspg.ssss.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.cspg.ssss.domain.DwdggbmjdTemporaryEntity;
import com.cspg.ssss.domain.UserDetailInfoEntity;
import com.cspg.ssss.service.DwdggbmjdTemporaryService;
import com.cspg.ssss.service.WxDepartmentService;
import com.cspg.ssss.vo.ResultVo;
import com.cspg.ssss.vo.ResultVoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


@RestController
@RequestMapping("/dataimport")
public class DataImportController {
    @Autowired
    private DwdggbmjdTemporaryService dwdggbmjdTemporaryService;
    @Autowired
    private WxDepartmentService departmentService;
    
    @RequestMapping(value = "/selectByid", method = RequestMethod.POST)
    public ResultVo selectByid(@RequestParam String id) {
        List<DwdggbmjdTemporaryEntity> s = dwdggbmjdTemporaryService.selectDataById(id);
        if (s.size() > 0) {
            return ResultVoUtil.success("查询成功", s, "400");
        } else {
            return ResultVoUtil.error("0000", "查询失败");
        }
    }

    @RequestMapping(value = "/UpdateByid", method = RequestMethod.POST)
    public ResultVo UpdateByid(@RequestBody DwdggbmjdTemporaryEntity dwdggbmjdTemporaryEntity) {
        try {
            HashMap<String, Object> condition = new HashMap<>();
            condition.put("userid", dwdggbmjdTemporaryEntity.getPersonName());
            List<UserDetailInfoEntity> departmentList = departmentService.getDepartmentUserList(condition);
            if (departmentList != null && departmentList.size() > 0) {
                dwdggbmjdTemporaryEntity.setDepartmentName(departmentList.get(0).getDepartment_displaypath());
            }
            String c = dwdggbmjdTemporaryEntity.getWtfxfs() + dwdggbmjdTemporaryEntity.getDwmc() + dwdggbmjdTemporaryEntity.getDepartmentName() +
                    dwdggbmjdTemporaryEntity.getWtms() + dwdggbmjdTemporaryEntity.getWtfxsj();
            //加密生成唯一id
            String id = DigestUtils.md5DigestAsHex(c.getBytes());
            DwdggbmjdTemporaryEntity dw = dwdggbmjdTemporaryService.selectDataId(id);
            if (dw != null) {
                String xh = dw.getXh();
                dwdggbmjdTemporaryEntity.setIsover("1");
                return ResultVoUtil.error("重复数据", "0000", dwdggbmjdTemporaryEntity);
            }
            int s = dwdggbmjdTemporaryService.updateData(dwdggbmjdTemporaryEntity);
            if (s > 0) {
                return ResultVoUtil.success("修改成功", "", "400");
            } else {
                return ResultVoUtil.error("9999", "修改失败，未修改数据");
            }
        } catch (Exception e) {
            return ResultVoUtil.error("9999", "修改失败", e);
        }
    }

    @RequestMapping(value = "/addMore", method = RequestMethod.POST)
    public ResultVo AddMore(@RequestBody JSONArray jSONArray) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss ");
        String v = JSONArray.toJSONString(jSONArray);
        List<DwdggbmjdTemporaryEntity> list = JSON.parseArray(v, DwdggbmjdTemporaryEntity.class);
        int s = 0;
        int a = 0;
        List<String> list4 = new ArrayList<>();
        DwdggbmjdTemporaryEntity dwdggbmjdTemporaryEntity;
        for (int i = 0; i < list.size(); i++) {
            HashMap<String, Object> condition = new HashMap<>();
            condition.put("userid", list.get(i).getPersonName());
            List<UserDetailInfoEntity> departmentList = departmentService.getDepartmentUserList(condition);
            if (departmentList != null && departmentList.size() > 0) {
                list.get(i).setDepartmentName(departmentList.get(0).getDepartment_displaypath());
            }
            //使用问题发现方式，单位名称，部门名称，问题描述，问题发现时间进行字符串组合
            String c = list.get(i).getWtfxfs() + list.get(i).getDwmc() + list.get(i).getDepartmentName() +
                    list.get(i).getWtms() + list.get(i).getWtfxsj();
            //加密生成唯一id
            String id = DigestUtils.md5DigestAsHex(c.getBytes());
            Date d = new Date();
            String b = sdf.format(d);
            Date now = null;
            try {
                now = sdf.parse(b);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            list.get(i).setId(id);
            list.get(i).setDrtime(now);
            if (list4.size() > 0) {
                for (String value : list4) {
                    if (id.equals(value)) {
                        list.get(i).setIsover("1");
                        a = a + 1;
                    }
                }
            }
            list4.add(id);
            dwdggbmjdTemporaryEntity = dwdggbmjdTemporaryService.selectDataId(id);
            if (dwdggbmjdTemporaryEntity != null) {
                a = a + 1;
                list.get(i).setIsover("1");
            }
        }
        if (a > 0) {
            return ResultVoUtil.error("0000", "保存问题中含有重复数据", list);
        }
        int cc = dwdggbmjdTemporaryService.batchInsert(list);
        if (cc > 0) {
            dwdggbmjdTemporaryEntity = list.get(0);
            List<DwdggbmjdTemporaryEntity> list2 = dwdggbmjdTemporaryService.selectAll(dwdggbmjdTemporaryEntity.getDrman());
            return ResultVoUtil.success("新增成功", list2, "400");
        } else {
            return ResultVoUtil.error("0000", "保存问题中含有重复数据", list);
        }
    }

    @RequestMapping(value = "/selectDateAll", method = RequestMethod.GET)
    public ResultVo selectAll(String id) {
        List<DwdggbmjdTemporaryEntity> list = dwdggbmjdTemporaryService.selectAll(id);
        if (list.size() > 0) {
            return ResultVoUtil.success("查询成功", list, "400");
        } else {
            return ResultVoUtil.error("1111", "查询成功,您没有待处理临时数据");
        }
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public ResultVo delete(String id) {
        int s = dwdggbmjdTemporaryService.delete(id);
        if (s > 0) {
            return ResultVoUtil.success("删除成功", "", "400");
        } else {
            return ResultVoUtil.error("0000", "删除失败");
        }
    }

    @RequestMapping(value = "/deleteList", method = RequestMethod.POST)
    public ResultVo deleteList(@RequestBody List<DwdggbmjdTemporaryEntity> list) {
        int s = dwdggbmjdTemporaryService.deleteByList(list);
        if (s > 0) {
            return ResultVoUtil.success("删除成功", "", "400");
        } else {
            return ResultVoUtil.error("0000", "删除失败");
        }
    }

}
